/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
import { isObject } from './typeof';

/**
 * create by clf on 2018-6-12
 * 进行深拷贝的函数
 * @param 需要进行拷贝的对象
 * @return 新的拷贝对象
 */
function deepClone<T extends AnyObject>(data: T): T
function deepClone<T extends AnyObject | Primitive>(data: Array<T>): Array<T>
function deepClone<T extends Primitive>(data: T): T
function deepClone (data: SafeAny): SafeAny {
    let obj: SafeAny;

    if (Array.isArray(data)) {
        obj = [];
        for (let i = 0, len = data.length; i < len; i++) {
            obj.push(deepClone(data[i]));
        }
    } else if (isObject(data)) {
        obj = Object.create(null);
        Object.keys(data).forEach(prop=>{
            obj[prop] = deepClone(data[prop] as SafeAny);
        });
    } else {

        // 不再具有下一层次
        return data;
    }

    return obj;
}

export {
    deepClone
};
